package com.zygh.questionnairesurvey.server;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.questionnairesurvey.common.Builder;
import com.zygh.questionnairesurvey.entity.Dept;
import com.zygh.questionnairesurvey.entity.Problem;
import com.zygh.questionnairesurvey.entity.Questionnaire;
import com.zygh.questionnairesurvey.entity.UserInfo;
import com.zygh.questionnairesurvey.entity.vo.QuestionsNumVo;
import com.zygh.questionnairesurvey.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @program: questionnaire-survey
 * @description: 统计Service
 * @author: ID-Tang
 * @create: 2021-05-18 08:51
 **/
@Service
public class StatisticalService {
    @Resource
    DeptMapper deptMapper;
    @Resource
    QuestionnaireMapper questionnaireMapper;
    @Resource
    ProblemMapper problemMapper;
    @Resource
    ProblemDetailsMapper problemDetailsMapper;
    @Resource
    UserInfoMapper userInfoMapper;

    /**
     * 统计问题数量占比
     *
     * @param deptId 部门id
     * @return 问题数量个数
     */
    public List<QuestionsNumVo> questionsNum(Integer deptId, Integer termStatus) {
        //用于接收通过父id查询出来的部门id
        List<Integer> listDeptId = new ArrayList<>();
        //0个问题
        int zero = 0;
        //1-3个问题个数
        int lessThan = 0;
        //4-6个问题个数
        int moreAndThan = 0;
        //7-9个问题个数
        int moreThanLessThan = 0;
        //10个及以上问题个数
        int moreThan = 0;
        List<QuestionsNumVo> results = new ArrayList<>();

        //如果部门id不等于空,则通过id查部门
        if (null != deptId) {
            Dept dept = deptMapper.selectById(deptId);
            //如果部门父id等于0,则将id做为父id查询部门id
            if (0 == dept.getPid()) {
                listDeptId = deptMapper.listDeptId(dept.getDeptId());
            }
            listDeptId.add(deptId);
        }

        //查询警号
        List<String> questionnaires = questionnaireMapper.listLowest(listDeptId, termStatus);

        if (ObjectUtils.isNotNull(questionnaires)){
            //查询最低级问题数量
            List<Integer> count = problemMapper.countByUserName(questionnaires);

            zero = problemMapper.countLowest(questionnaires);

            for (Integer integer : count) {
                if (integer > 0 && integer < 4) {
                    lessThan += 1;
                    continue;
                }
                if (integer > 3 && integer < 7) {
                    moreAndThan += 1;
                    continue;
                }
                if (integer > 6 && integer < 10) {
                    moreThanLessThan += 1;
                    continue;
                }
                if (integer > 9) {
                    moreThan += 1;
                }
            }
        }

        QuestionsNumVo numVo = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "0个")
                .with(QuestionsNumVo::setCount, zero)
                .build();
        results.add(numVo);
        QuestionsNumVo questionsNumVo = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "1-3个")
                .with(QuestionsNumVo::setCount, lessThan)
                .build();
        results.add(questionsNumVo);
        QuestionsNumVo questionsNum = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "4-6个")
                .with(QuestionsNumVo::setCount, moreAndThan)
                .build();
        results.add(questionsNum);
        QuestionsNumVo questionsVo = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "7-9个")
                .with(QuestionsNumVo::setCount, moreThanLessThan)
                .build();
        results.add(questionsVo);
        QuestionsNumVo questions = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "10个及以上")
                .with(QuestionsNumVo::setCount, moreThan)
                .build();
        results.add(questions);

        return results;
    }

    public List<QuestionsNumVo> questionsNums(Integer deptId, Integer termStatus) {
        List<QuestionsNumVo> results = new ArrayList<>();
        //0个问题
        int zero = 0;
        //1-3个问题个数
        int lessThan = 0;
        //4-6个问题个数
        int moreAndThan = 0;
        //7-9个问题个数
        int moreThanLessThan = 0;
        //10个及以上问题个数
        int moreThan = 0;
        List<UserInfo> userByDeptId = userInfoMapper.findUserByDeptId(deptId, termStatus);
        for (UserInfo userInfo : userByDeptId) {
            int problemCoundById = problemMapper.findProblemCoundById(userInfo.getUserName());
            if (problemCoundById == 0) {
                zero += 1;
                continue;
            }
            if (problemCoundById > 0 && problemCoundById < 4) {
                lessThan += 1;
                continue;
            }
            if (problemCoundById > 3 && problemCoundById < 7) {
                moreAndThan += 1;
                continue;
            }
            if (problemCoundById > 6 && problemCoundById < 10) {
                moreThanLessThan += 1;
                continue;
            }
            if (problemCoundById > 9) {
                moreThan += 1;
            }

        }

        QuestionsNumVo numVo = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "0个")
                .with(QuestionsNumVo::setCount, zero)
                .build();
        results.add(numVo);
        QuestionsNumVo questionsNumVo = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "1-3个")
                .with(QuestionsNumVo::setCount, lessThan)
                .build();
        results.add(questionsNumVo);
        QuestionsNumVo questionsNum = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "4-6个")
                .with(QuestionsNumVo::setCount, moreAndThan)
                .build();
        results.add(questionsNum);
        QuestionsNumVo questionsVo = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "7-9个")
                .with(QuestionsNumVo::setCount, moreThanLessThan)
                .build();
        results.add(questionsVo);
        QuestionsNumVo questions = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "10个及以上")
                .with(QuestionsNumVo::setCount, moreThan)
                .build();
        results.add(questions);

        return results;
    }


    /**
     * 查询某个问题下各大队人数
     *
     * @param lowest 问题id
     * @return 统计返回类
     */
    public List questionsPeople(Integer lowest, Integer termStatus) {
        List<QuestionsNumVo> results = new ArrayList<>();
        //去重查出问题下关联的所有大队
        List<Dept> byLowestAndDistinct = deptMapper.findByLowestAndDistinct(lowest);
        //去重查出子部门id
        List<Integer> deptId = deptMapper.findDeptId(lowest);
        if (deptId.size() > 0) {
            //查出父部门
            List<Dept> selectList = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                    .in(Dept::getDeptId, deptId));
            byLowestAndDistinct.addAll(selectList);
        }

        if (byLowestAndDistinct.size() < 1) {
            return results;
        }

        //客户要求写死数据
        QuestionsNumVo questionsNum = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "交警支队")
                .with(QuestionsNumVo::setCount, questionnaireMapper.listPeopleOnLowest(lowest, termStatus))
                .build();
        results.add(questionsNum);

        byLowestAndDistinct.forEach(dept -> {
            //通过父id是该大队查用户
            Integer countByPid = questionnaireMapper.
                    listPeopleOnPid(lowest, dept.getDeptId(), termStatus);
            //通过大队查用户
            Integer countOnDept = questionnaireMapper.
                    listPeopleOnDept(lowest, dept.getDeptId(), termStatus);

            QuestionsNumVo questionsNumVo = Builder.of(QuestionsNumVo::new)
                    .with(QuestionsNumVo::setName, dept.getName())
                    .with(QuestionsNumVo::setDeptId, dept.getDeptId())
                    .with(QuestionsNumVo::setCount, countByPid + countOnDept)
                    .build();
            results.add(questionsNumVo);
        });

        LinkedHashSet<QuestionsNumVo> set = new LinkedHashSet<>(results);
        results.clear();
        results.addAll(set);

        return results;
    }

    /**
     * 统计单个问题下各大队问题详情条数
     *
     * @param encoder 问题编号
     * @return 各大队问题详情条数
     */
    public List detailNum(String encoder, Integer termStatus) {
        List<QuestionsNumVo> results = new ArrayList<>();
        //查出所有父级部门
        List<Dept> deptList = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getPid, 0));

        //客户要求写死数据
        QuestionsNumVo questionsNum = Builder.of(QuestionsNumVo::new)
                .with(QuestionsNumVo::setName, "交警支队")
                .with(QuestionsNumVo::setCount, problemDetailsMapper.countByWrapper(encoder, termStatus, null))
                .build();
        results.add(questionsNum);

        for (Dept dept : deptList) {
            List<Integer> deptId = deptMapper.listDeptId(dept.getDeptId());
            deptId.add(dept.getDeptId());

            //查出所有警号
            List<String> userName = userInfoMapper.listUserName(deptId);

            //通过警号查数量
            Integer count = problemDetailsMapper.countByWrapper(encoder, termStatus, userName);

            if (count < 1) {
                continue;
            }

            QuestionsNumVo questionsNumVo = Builder.of(QuestionsNumVo::new)
                    .with(QuestionsNumVo::setName, dept.getName())
                    .with(QuestionsNumVo::setDeptId, dept.getDeptId())
                    .with(QuestionsNumVo::setCount, count)
                    .build();

            results.add(questionsNumVo);
        }

        return results;
    }
}

